package com.qianfeng.tiantian_banjia.utils;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.content.Context;

public class HttpClient {

	private static DefaultHttpClient mHttpClient;

	/**
	 * 鍒涘缓httpClient瀹炰緥
	 */
	public static synchronized DefaultHttpClient getHttpClient(Context context)
			throws KeyStoreException, NoSuchAlgorithmException,
			CertificateException, IOException, KeyManagementException,
			UnrecoverableKeyException {
		if (null == mHttpClient) {
			HttpParams params = new BasicHttpParams();
			// 璁剧疆涓�簺鍩烘湰鍙傛暟
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

			HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);
			/* 璇锋眰瓒呮椂 */
			HttpConnectionParams.setSoTimeout(params, 30 * 1000);
			// 璁剧疆鎴戜滑鐨凥ttpClient鏀寔HTTP鍜孒TTPS涓ょ妯″紡
			SchemeRegistry schReg = new SchemeRegistry();
			schReg.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));

			// 浣跨敤绾跨▼瀹夊叏鐨勮繛鎺ョ鐞嗘潵鍒涘缓HttpClient
			ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
					params, schReg);
			mHttpClient = new DefaultHttpClient(conMgr, params);
		}
		return mHttpClient;
	}

}
